package com.example.blackjack;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	//cover img
	private Drawable casino_back_image;
	private Drawable player_back_image;
	
	private TextView score_text;
	private TextView beat_text;
	private TextView cash_text;
	
	//cheps
	private Map<Integer, Integer> cheeps;
	private ImageView cheep5_view;
	private ImageView cheep10_view;
	private ImageView cheep25_view;
	private ImageView cheep100_view;
	
	//play table
	private List<ImageView> casino_views;
	private List<ImageView> player_views;
	
	//buttons
	private Button deal_button;
	private Button hit_button;
	private Button stand_button;
	
	//imgs of cards
	private List<Card> cards_images;
	
	//values
	private int cash_value;
	private int beat_value;
	
	//mode
	//0 - choose cheep
	//1 - choose cheep, make deal
	//2 - hit
	//3 - stand
	int mode;
	
	private int casino_cards;
	private int player_cards;
	
	private int casino_value;
	private int player_value;
	
	private int must_stand;
	private int black_jack;
	
	//listeners
	private CheepListener cheep_listener;
	private DealListener deal_listener;
	private HitListener hit_listener;
	private StandListener stand_listener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//cover img
		casino_back_image = getResources().getDrawable(R.drawable.cb);
		player_back_image = getResources().getDrawable(R.drawable.pb);
		
		//cheeps
		cheeps = new HashMap<Integer, Integer>();
		cheep_listener = new CheepListener();
		
		cheep5_view = (ImageView) findViewById(R.id.cheep_5); 
		cheep10_view = (ImageView) findViewById(R.id.cheep_10); 
		cheep25_view = (ImageView) findViewById(R.id.cheep_25);
		cheep100_view = (ImageView) findViewById(R.id.cheep_100);
		
		cheep5_view.setOnClickListener(cheep_listener);
		cheep10_view.setOnClickListener(cheep_listener);
		cheep25_view.setOnClickListener(cheep_listener);
		cheep100_view.setOnClickListener(cheep_listener);
		
		cheeps.put(cheep5_view.getId(), 5);
		cheeps.put(cheep10_view.getId(), 10);
		cheeps.put(cheep25_view.getId(), 25);
		cheeps.put(cheep100_view.getId(), 100);
		
		//play table
		casino_views = new ArrayList<ImageView>();
		casino_views.add((ImageView) findViewById(R.id.CasinoCard01));
		casino_views.add((ImageView) findViewById(R.id.CasinoCard02));
		casino_views.add((ImageView) findViewById(R.id.CasinoCard03));
		casino_views.add((ImageView) findViewById(R.id.CasinoCard04));
		casino_views.add((ImageView) findViewById(R.id.CasinoCard05));
		
		player_views = new ArrayList<ImageView>();
		player_views.add((ImageView) findViewById(R.id.PlayerCard01));
		player_views.add((ImageView) findViewById(R.id.PlayerCard02));
		player_views.add((ImageView) findViewById(R.id.PlayerCard03));
		player_views.add((ImageView) findViewById(R.id.PlayerCard04));
		player_views.add((ImageView) findViewById(R.id.PlayerCard05));
		
		casino_cards = 0;
		player_cards = 0;
		
		//buttons
		deal_listener = new DealListener();
		hit_listener = new HitListener();
		stand_listener = new StandListener();
		
		deal_button = (Button) findViewById(R.id.cheep_deal);
		hit_button = (Button) findViewById(R.id.hit_button);
		stand_button = (Button) findViewById(R.id.stand_button);
		
		deal_button.setOnClickListener(deal_listener);
		hit_button.setOnClickListener(hit_listener);
		stand_button.setOnClickListener(stand_listener);
		
		//score
		score_text = (TextView) findViewById(R.id.score_value);
		cash_text = (TextView) findViewById(R.id.cash_value);
		beat_text = (TextView) findViewById(R.id.bet_value);
		
		cash_value = 500;
		beat_value = 0;
		
		casino_value = 0;
		player_value = 0;
		
		must_stand = 17;
		black_jack = 21;
		
		mode = 0;
		
		initStack();
		
	}
	
	private void initStack() {
		
		//avalable cards 
		cards_images = new ArrayList<Card>();
		
		cards_images.add(new Card(getResources().getDrawable(R.drawable.b2), 2));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.b3), 3));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.b4), 4));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.b5), 5));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.b6), 6));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.b7), 7));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.b8), 8));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.b9), 9));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.b10), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.bj), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.bq), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.bk), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.ba), 11));
		
		cards_images.add(new Card(getResources().getDrawable(R.drawable.c2), 2));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.c3), 3));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.c4), 4));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.c5), 5));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.c6), 6));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.c7), 7));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.c8), 8));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.c9), 9));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.c10), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.cj), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.cq), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.ck), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.ca), 11));
		
		cards_images.add(new Card(getResources().getDrawable(R.drawable.k2), 2));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.k3), 3));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.k4), 4));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.k5), 5));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.k6), 6));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.k7), 7));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.k8), 8));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.k9), 9));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.k10), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.kj), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.kq), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.kk), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.ka), 11));
		
		cards_images.add(new Card(getResources().getDrawable(R.drawable.p2), 2));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.p3), 3));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.p4), 4));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.p5), 5));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.p6), 6));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.p7), 7));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.p8), 8));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.p9), 9));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.p10), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.pj), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.pq), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.pk), 10));
		cards_images.add(new Card(getResources().getDrawable(R.drawable.pa), 11));
				
		long seed = System.nanoTime();
		Collections.shuffle(cards_images, new Random(seed));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	class CheepListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (mode == 0) {
				for(ImageView casino_item: casino_views) {
					casino_item.setBackgroundDrawable(casino_back_image);
				}
				
				for(ImageView player_item: player_views) {
					player_item.setBackgroundDrawable(player_back_image);
				}
			}
			
			int value = cheeps.get(v.getId());
			int delta = cash_value-beat_value;
			if ((mode == 0 || mode == 1) && delta >= 0) {
				beat_value = beat_value + value;
				
				cash_text.setText(Integer.toString(cash_value-beat_value));
				beat_text.setText(Integer.toString(beat_value));
				mode = 1;
			}
		}
	}
	
	class DealListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			
			if (mode == 1) {
				mode = 2;
				
				playPlayer();
				playPlayer();
				playCasino();
				score_text.setText("Do Hit or Stand");
				return;
			}
			
		}
	}
	
	class HitListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			
			if (mode == 2 || mode == 3) {
				mode = 3;
				playPlayer();
				return;
			}
			
		}
	}
	
	class StandListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			
			if (mode == 2 || mode == 3) {
				runCasino();
				return;
			}
		}
	}
	
	private void playPlayer() {
		if (cards_images.isEmpty()) {
			initStack();
		}
		
		Card card = cards_images.get(cards_images.size() - 1);
		cards_images.remove(card);
		Drawable card_img = card.getImage();
		
		ImageView player_view = player_views.get(player_cards);
		player_view.setBackgroundDrawable(card_img);
		
		player_value = player_value + card.getValue();
		player_cards = player_cards +1;
		
		if (player_value == black_jack) {
			runCasino();
			return;
		}
		
		if (player_value > black_jack) {
			reset(0);
			return;
		}
		
	}
	
	private void runCasino() {
		while (casino_value < player_value || casino_value <= must_stand) {
			playCasino();
		}
		
		if (casino_value > black_jack) {
			reset(2);
			return;
		}
		
		if (casino_value > player_value) {
			reset(0);
			return;
		}
		
		if (casino_value < player_value) {
			reset(2);
			return;
		}
	
		//dead heat
		reset(1);
		return;
	}
	
	private void playCasino() {
		
		if (cards_images.isEmpty()) {
			initStack();
		}
		
		Card card = cards_images.get(cards_images.size() - 1);
		cards_images.remove(card);
		Drawable card_img = card.getImage();
		
		ImageView casino_view = casino_views.get(casino_cards);
		casino_view.setBackgroundDrawable(card_img);
		
		casino_value = casino_value + card.getValue();
		casino_cards = casino_cards +1;
	}
	
	private void reset(int win) {
		if (win == 2) {
			score_text.setText("You win! Make you Bet");
			cash_value = cash_value + beat_value;
			cash_text.setText(Integer.toString(cash_value));
		} 
		
		if (win == 0) {
			String prefix = (cash_value > 0) ? "Make you Bet" : "Go to home ";
			cash_value = cash_value - beat_value;
			score_text.setText("You lose! "+prefix);
		}
		
		if (win == 1) {
			score_text.setText("Dead heat! Make you Bet");
			cash_text.setText(Integer.toString(cash_value));
		}
		
		casino_cards = 0;
		player_cards = 0;
		
		beat_value = 0;
		
		casino_value = 0;
		player_value = 0;
		
		mode = 0;
		
		cash_text.setText(Integer.toString(cash_value));
		beat_text.setText(Integer.toString(beat_value));
	}

}
